package com.ruoyi.platform.system.user.controller;

import com.ruoyi.base.annotaion.PermissionCheck;
import com.ruoyi.base.controller.BaseController;
import com.ruoyi.base.page.PageData;
import com.ruoyi.base.permission.TokenManager;
import com.ruoyi.base.pojo.HttpResult;
import com.ruoyi.base.utils.arrays.ArrayUtils;
import com.ruoyi.base.utils.inputcheck.InputCheckUtils;
import com.ruoyi.platform.system.menu.enums.MenuEnum;
import com.ruoyi.platform.system.menu.pojo.MenuTreeDTO;
import com.ruoyi.platform.system.user.pojo.UserDO;
import com.ruoyi.platform.system.user.pojo.UserDTO;
import com.ruoyi.platform.system.user.pojo.UserEditQuery;
import com.ruoyi.platform.system.user.pojo.UserListDTO;
import com.ruoyi.platform.system.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理
 *
 * @author lcf
 * @version 0.0.1
 * @since 2020-11-13 05:40:22
 */
@Api(tags = {"用户管理"})
@RestController
@Scope("request")
@RequestMapping("/sys/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private TokenManager tokenService;

    /**
     * 获取用户列表
     */
    @ApiOperation("获取用户列表")
    @PermissionCheck(MenuEnum.USER_MANAGEMENT_SELECT)
    @GetMapping("/list")
    public HttpResult<PageData<UserDO, UserListDTO>> getUserPlatList(@RequestParam(defaultValue = "1") @ApiParam("页码") int pageNum,
                                                                     @RequestParam(defaultValue = "20") @ApiParam("每页条目数") int pageSize,
                                                                     @ApiParam("昵称") String nickName) {
        return data(userService.getUserPlatList(pageNum, pageSize, nickName));
    }

    /**
     * 获取用户详情
     */
    @ApiOperation("获取用户详情")
    @PermissionCheck(MenuEnum.USER_MANAGEMENT_SELECT)
    @GetMapping("/{userId}")
    public HttpResult<UserDTO> getUserDetail(@ApiParam(value = "用户id", required = true) @PathVariable long userId) {
        InputCheckUtils.checkLong(userId, "请选择用户");

        return data(userService.getUserDetail(userId));
    }

    /**
     * 获取当前用户菜单权限列表
     */
    @ApiOperation("获取当前用户菜单权限列表")
    @GetMapping("/user/menu/list")
    public HttpResult<List<MenuTreeDTO>> getRoleMenuTree() {
        return data(userService.getRoleMenuTree(tokenService.getLoginUserId()));
    }

    /**
     * 获取我的详情
     */
    @ApiOperation("获取我的详情")
    @GetMapping("/current")
    public HttpResult<UserDTO> getUserCurrentDetail() {
        return data(userService.getUserDetail(tokenService.getLoginUserId()));
    }

    /**
     * 修改用户
     */
    @ApiOperation("修改用户")
    @PatchMapping("/{userId}")
    @PermissionCheck(MenuEnum.USER_MANAGEMENT_EDIT)
    public HttpResult<String> editUser(@PathVariable @ApiParam("用户id") long userId,
                                       UserEditQuery query,
                                       @ApiParam("角色code列表") Long[] roleCodeList) {
        InputCheckUtils.checkStr(query.getNickName(), "请输入姓名");
        InputCheckUtils.checkArray(roleCodeList, "请选择角色");

        userService.editUser(userId, query, ArrayUtils.arrays2List(roleCodeList));
        return success();
    }

    /**
     * 修改我的信息
     */
    @ApiOperation("修改我的信息")
    @PatchMapping
    public HttpResult<String> editUser(UserEditQuery query) {
        InputCheckUtils.checkStr(query.getNickName(), "请输入姓名");

        userService.editUser(query);
        return success();
    }

    /**
     * 修改账号状态
     */
    @ApiOperation("修改账号状态")
    @PermissionCheck(MenuEnum.USER_MANAGEMENT_EDIT)
    @PatchMapping("/account/status")
    public HttpResult<String> editAccountStatus(@ApiParam("用户id") long userId,
                                                @ApiParam(value = "账号状态0正常1停用", required = true) int accountStatus) {
        InputCheckUtils.checkLong(userId, "请选择用户");

        userService.editAccountStatus(userId, accountStatus);
        return success();
    }

    /**
     * 删除用户
     */
    @ApiOperation("删除用户")
    @PermissionCheck(MenuEnum.USER_MANAGEMENT_DEL)
    @DeleteMapping("/{userId}")
    public HttpResult<String> del(@ApiParam(value = "用户id", required = true) @PathVariable long userId) {
        InputCheckUtils.checkLong(userId, "请选择用户");

        userService.del(userId);

        return success();
    }
}